package com.yuoj.yuojbackendjudgeservice.rabbitmq;

import com.rabbitmq.client.Channel;
import com.yuoj.yuojbackendjudgeservice.JudgeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 刘渠好
 * @since 2025-04-16 21:44
 * 消息队列-消费者
 */
@Component
@Slf4j
public class MyMessageConsumer {

    @Resource
    private JudgeService judgeService;

    @SneakyThrows
    @RabbitListener(queues = {"code_queue"},ackMode = "MANUAL")  //mq监听器
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        //获取消费信息
        log.info ("receive message: {}",message);
        long questionSubmitId = Long.parseLong (message);
        try {
            judgeService.doJudge (questionSubmitId);
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            channel.basicNack (deliveryTag,false,false);
        }

    }
}
